package cn.szzsi.service;

import cn.szzsi.common.exception.BusinessException;

import cn.szzsi.common.kit.MathUtil;
import cn.szzsi.common.model.*;
import cn.szzsi.common.model.zshop.PlateformConfig;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service实现类 - 支付相关的service
 * 
 */
public class FenxiaoService {
	
	public static final FenxiaoService service = new FenxiaoService();
    public static Map<String,Map<Integer,Integer>> fxCache = null;
    private static final String SYMOBL = ",";


    //商户
    public void fenxiaoHandle(Member member, Orders orders) throws BusinessException {
        if(member == null)
            throw new BusinessException("用户错误");
        //找到指定级别的父级人员
        BigDecimal fee = orders.getTotalAmount();
        PlateformConfig plateformConfig=PlateformConfig.dao.findById(1);
        String sharePercent=plateformConfig.getSharePercent();
        if(StrKit.isBlank(sharePercent)){
            sharePercent="3";//默认3%
        }
        BigDecimal percent=(new BigDecimal(sharePercent)).divide(new BigDecimal(100));
        BigDecimal fenxiaoFee=percent.multiply(fee);
        fenxiaoFee=fenxiaoFee.setScale(2,BigDecimal.ROUND_HALF_DOWN);
        if(StrKit.notBlank(member.getPid())&& !"0".equals(member.getPid())){
            incMemberInCome(fenxiaoFee,member.getPid(),member,1,Integer.valueOf(sharePercent),orders);
        }

    }
    public void incMemberInCome(BigDecimal fee, String mid, Member orgin, int level, int rate, Orders orders){
        Member m = Member.dao.findById(mid);
        if (m == null){
            return;
        }
        UserBalanceService.service.updateBalanceForFenXiao(m,fee,1);
        UserBalanceDetailService.service.saveFenXiaoInfo(mid,fee, UserBalanceDetail.BalanceType.fenXiao.ordinal(),"",orders.getId(),orgin.getId(),level,rate);
    }
    //商户


    //平台**********************************
    public void fenxiaoHandle(Member member, OrdersPlateform ordersPlateform) throws BusinessException {
        if(member == null)
            throw new BusinessException("用户错误");
        //找到指定级别的父级人员
        BigDecimal fee = ordersPlateform.getTotalAmount();
        PlateformConfig plateformConfig=PlateformConfig.dao.findById(1);
        String sharePercent=plateformConfig.getSharePercent();
        if(StrKit.isBlank(sharePercent)){
            sharePercent="3";//默认3%
        }
        BigDecimal percent=(new BigDecimal(sharePercent)).divide(new BigDecimal(100));
        BigDecimal fenxiaoFee=percent.multiply(fee);
        fenxiaoFee=fenxiaoFee.setScale(2,BigDecimal.ROUND_HALF_DOWN);
        if(StrKit.notBlank(member.getPid())){
            incMemberInCome(fenxiaoFee,member.getPid(),member,1,Integer.valueOf(sharePercent),ordersPlateform);
        }

    }

	public void incMemberInCome(BigDecimal fee, String mid, Member orgin, int level, int rate, OrdersPlateform ordersPlateform){
        Member m = Member.dao.findById(mid);
        if (m == null){
            return;
        }
        //1，给会员充值
        UserBalanceService.service.updateBalanceForFenXiao(m,fee,1);
        //添加明细表
        UserBalanceDetailService.service.saveFenXiaoInfo(mid,fee, UserBalanceDetail.BalanceType.fenXiao.ordinal(),"",ordersPlateform.getId(),orgin.getId(),level,rate);

    }
    //平台**********************************


}
